package com.project.thisDatabase.base.model;

import com.google.gson.reflect.TypeToken;
import com.project.annotation.model.base.RequestBase;
import com.project.thisCommon.common.ConvertEntity;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;

public class DBEntity<T> extends DBContext {

    public DBEntity() {
    }

    public DBEntity(RequestBase<?> entity) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Type tp = new TypeToken<T>() {
        }.getType();
        Class cls = ConvertEntity.GetClass(tp);
        T t = (T)cls.newInstance();
        BeanUtils.copyProperties(entity.getBody(), t);
        this.setAppId(entity.getAppId());
        this.setOth(entity.getOth());
        this.setToken(entity.getToken());
        this.setUserId(entity.getUserId());
        this.setVersion(entity.getVersion());

        this.setBody(t);
    }

    public DBEntity(T t, RequestBase<?> entity) throws InvocationTargetException, IllegalAccessException {
        BeanUtils.copyProperties(entity.getBody(), t);
        this.setAppId(entity.getAppId());
        this.setOth(entity.getOth());
        this.setToken(entity.getToken());
        this.setUserId(entity.getUserId());
        this.setVersion(entity.getVersion());

        this.setBody(t);
    }

    public DBEntity(T t, DBEntity<?> entity) throws InvocationTargetException, IllegalAccessException {
        BeanUtils.copyProperties(entity.getBody(), t);
        this.setAppId(entity.getAppId());
        this.setFunc(entity.getFunc());
        this.setMethod(entity.getMethod());
        this.setOth(entity.getOth());
        this.setToken(entity.getToken());
        this.setUserId(entity.getUserId());
        this.setVersion(entity.getVersion());

        this.setBody(t);
    }

    private T body;

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

}
